﻿using System;
using System.Web;
using viForum.Classes;
using viForum.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_viForum_Editor_Editor : WebModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            //set forum theme
            string themeCSS = WebRoots.GetModulesRoot + "viForum/Themes/" + BaseController.ForumTheme + "/viForum.css";
            WebPageController.AddCssLink(Page, themeCSS, true);

            ForumId.Value = Request.QueryString["ForumId"];
            ThreadId.Value = Request.QueryString["ThreadId"];
            if (Request.QueryString["AddReply"] != null)
            {
                int threadid = 0;
                if (Int32.TryParse(Request.QueryString["ThreadId"], out threadid))
                {
                    Thread thread = ThreadController.GetThread(threadid);
                    Title.Value = thread.Title;
                    Title.Disabled = true;
                }                
            }
        }
        else
            return;
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            if (Request.QueryString["AddThread"] != null)
                AddThread();
            if (Request.QueryString["AddReply"] != null)
                AddReply();
        }
    }

    private void AddReply()
    {
        int forumid = 0;
        if (Int32.TryParse(ForumId.Value, out forumid))
        {
            Forum forum = ForumController.GetForum(forumid);
            if (forum != null)
            {
                string CurrentUser = System.Threading.Thread.CurrentPrincipal.Identity.Name;
                Reply reply = new Reply();
                reply.ForumId = forumid;
                reply.ThreadId = Convert.ToInt32(ThreadId.Value);
                reply.Content = Content.Text;
                reply.UserName = CurrentUser;
                reply.Date = DateTime.Now;
                int newReplyId = ReplyController.AddReply(reply);
                if (newReplyId > 0)
                {
                    // update user data
                    User user = UserController.GetUser(CurrentUser);
                    if (user == null)
                    {
                        // add this user as forum user
                        UserController.AddUser(new User(CurrentUser, 1));
                    }
                    else
                    {
                        user.PostsCount += 1;
                        UserController.UpdateUser(user);
                    }

                    // goto the newly added reply page
                    Thread thread = ThreadController.GetThread(reply.ThreadId);
                    // thread URL = Forum/Thread/ThreadId/ThreadTitle .aspx
                    Response.Redirect(WebRoots.GetWebRoot + BaseController.viForumUrlSeparator + "/Thread/" + thread.ThreadId + "/" + thread.Title + ".aspx");
                }
            }
        }
    }

    protected void Cancel_Click(object sender, EventArgs e)
    {
        Title.Value = "";
        Content.Text = "";
    }

    private void AddThread()
    {        
        if (!string.IsNullOrEmpty(ForumId.Value))
        {
            int forumid = 0;
            if (Int32.TryParse(ForumId.Value, out forumid))
            {
                Forum forum = ForumController.GetForum(forumid);
                if (forum != null)
                {
                    string CurrentUser = System.Threading.Thread.CurrentPrincipal.Identity.Name;
                    Thread thread = new Thread();
                    thread.ForumId = forumid;
                    if (string.IsNullOrEmpty(Title.Value))
                        return;
                    thread.Title = RemoveIllegalCharacters(Title.Value);
                    if (string.IsNullOrEmpty(Content.Text))
                        return;
                    thread.Content = Content.Text;
                    thread.UserName = CurrentUser;
                    thread.Date = DateTime.Now;
                    int newThreadId = 0;
                    newThreadId = ThreadController.AddThread(thread);
                    if (newThreadId > 0)
                    {
                        // update user data
                        User user = UserController.GetUser(CurrentUser);
                        if (user == null)
                        {
                            // add this user as forum user
                            UserController.AddUser(new User(CurrentUser, 1));
                        }
                        else
                        {
                            user.PostsCount += 1;
                            UserController.UpdateUser(user);
                        }
                        // goto the newly added thread page
                        Thread newThread = ThreadController.GetThread(newThreadId);
                        // thread URL = Forum/Thread/ThreadId/ThreadTitle .aspx
                        Response.Redirect(WebRoots.GetWebRoot + BaseController.viForumUrlSeparator + "/Thread/" + newThread.ThreadId + "/" + newThread.Title + ".aspx");
                    }
                }
            }
        }
    }

    private static string RemoveIllegalCharacters(string Title)
    {
        if (string.IsNullOrEmpty(Title))
            return Title;

        Title = Title.Replace(":", string.Empty);
        Title = Title.Replace("/", string.Empty);
        Title = Title.Replace("?", string.Empty);
        Title = Title.Replace("#", string.Empty);
        Title = Title.Replace("[", string.Empty);
        Title = Title.Replace("]", string.Empty);
        Title = Title.Replace("@", string.Empty);
        Title = Title.Replace("*", string.Empty);
        Title = Title.Replace(".", string.Empty);
        Title = Title.Replace(",", string.Empty);
        Title = Title.Replace("\"", string.Empty);
        Title = Title.Replace("&", string.Empty);
        Title = Title.Replace("'", string.Empty);
        Title = Title.Replace(" ", "-");
        return HttpUtility.UrlEncode(Title).Replace("%", string.Empty);
    }
}